package com.ckv.qcm.model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ckv.qcm.model.Answer;

public class AnswerDAO extends DAO {

	public static ArrayList<Answer> findByQuestion(long idQuestion) {
		ArrayList<Answer> answers = new ArrayList<Answer>();
		try {
			ResultSet result = connection.createStatement().executeQuery(
					"SELECT * FROM Reponses WHERE idQuestion = " + idQuestion);
			while (result.next())
				answers.add(new Answer(result.getLong("idReponse"), result
						.getLong("idQuestion"), result.getString("libelle"),
						result.getBoolean("isTrue")));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return answers;
	}

	public Answer find(long id, long idQuestion) {
		Answer answer = null;
		try {
			ResultSet result = connection.createStatement().executeQuery(
					"SELECT * FROM Reponses WHERE idReponse = " + id
							+ " AND idQuestion = " + idQuestion);
			if (result.next())
				answer = new Answer(id, result.getString("libelle"),
						result.getBoolean("isTrue"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return answer;
	}

	public ArrayList<Answer> findAll() {
		ArrayList<Answer> answers = new ArrayList<Answer>();
		try {
			ResultSet result = connection.createStatement().executeQuery(
					"SELECT * FROM Reponses");
			while (result.next()) {
				answers.add(new Answer(result.getLong("idReponse"), result
						.getString("libelle"), result.getBoolean("isTrue")));
			}
			return answers;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Answer create(Answer answer) {
		PreparedStatement preparedStatement = null;
		ResultSet generatedKeys = null;

		try {

			preparedStatement = connection
					.prepareStatement(
							"INSERT INTO Reponses (idReponse, idQuestion, libelle, isTrue) VALUES (?, ?, ?, ?)",
							Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setLong(1, answer.getId());
			preparedStatement.setLong(2, answer.getIdQuestion());
			preparedStatement.setString(3, answer.getStatement());
			preparedStatement.setBoolean(4, answer.isValid());

			int affectedRows = preparedStatement.executeUpdate();
			if (affectedRows == 0) {
				throw new SQLException(
						"Creating answer failed, no rows affected.");
			}

			generatedKeys = preparedStatement.getGeneratedKeys();
			if (generatedKeys.next()) {
				answer = this.find(generatedKeys.getLong(1));
			} else {
				throw new SQLException(
						"Creating answer failed, no generated key obtained.");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return answer;
	}

	public Answer update(Answer answer) {
		try {
			this.connection.createStatement().executeUpdate(
					"UPDATE Reponses SET libelle = '" + answer.getStatement()
							+ "' isTrue = " + answer.isValid()
							+ " WHERE idReponse = " + answer.getId());
			answer = this.find(answer.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return answer;
	}

	public void delete(Answer answer) {
		try {
			this.connection.createStatement().executeUpdate(
					"DELETE FROM Reponses WHERE idReponse = " + answer.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Answer find(long id) {
		// TODO Auto-generated method stub
		return null;
	}

}
